﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Helix_Network_Supervisor_3
{
    public partial class Wizard : Form
    {
        #region Fields

        private FileExplorer fileExplorer = new FileExplorer();
        private TreeNode rootNode;
        private string completePath = "";
        private int currentStep = 0;
        private string _vixenPath = "";
        private string _helixPath = "";
        private string _audioPath = "";
        private string _mp3ChipVersion = "";

        #endregion Fields

        #region Ctor

        public Wizard()
        {
            InitializeComponent();
            Step0();
        }

        #endregion Ctor

        #region Wizard Steps

        private void Step0()
        {
            currentStep = 0;
            previousButton.Enabled = false;
            skipButton.DialogResult = DialogResult.Cancel;

            resultLabel.Visible = false;

            groupBox1.Text = "Setup Wizard";
            whatLabel.Enabled = true;
            whatRichTextBox.Text = "This is the Helix Network Supervisor Setttings Setup Wizard.  " +
                "This is a 4 step wizard that will allow you to identify various directories that the HNS needs to function properly";

            whyLabel.Enabled = true;
            whyRichTextBox.Text = "The HNS needs to know where the Vixen data is stored on your hard drive as well as where " +
                "you want to store the HNS files.";

            wizardTreeView.Nodes.Clear();
        }

        private void Step1()
        {
            currentStep = 1;
            previousButton.Enabled = true;
            skipButton.DialogResult = DialogResult.None;

            resultLabel.Text = "Vixen Path";
            resultLabel.Visible = true;

            groupBox1.Text = "Step 1 of 4:  Vixen Path";

            whatRichTextBox.Text = "Where the Vixen files are stored on your hard drive?  This is usually where the Vixen " +
                "application is stored or in your Documents directory in a directory titled Vixen.";


            whyRichTextBox.Text = "The Helix Network Supervisor needs to know where Vixen stores " +
                "the Sequence files, Audio files and Profile files.";

            rootNode = fileExplorer.PopulateTreeView(wizardTreeView);
            resultTextBox.Text = _vixenPath;
        }

        private bool Step1_Validation()
        {
            string testPath = _vixenPath + @"\Audio";

            if (!Directory.Exists(testPath))
            {
                MessageBox.Show("Unable to verify that this is a valid Vixen directory." +
                    "\rPlease make sure to select the main Vixen directory where all of the subdirectories are stored" +
                    "\r\rPlease try again.",
                            "Helix Setup Wizard Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return false;
            }
            return true;
        }

        private void Step2()
        {
            currentStep = 2;
            resultLabel.Text = "Helix Network Supervisor Path";

            groupBox1.Text = "Step 2 of 4:  Helix Network Supervisor Path";

            whatRichTextBox.Text = "Where do you want the Helix Network Supervisor to store its files?  " +
                "Your Documents directory is recommended.";

            whyRichTextBox.Text = "The Helix Network Supervisor creates several different types of files.  " +
                "These files need to be stored in one location.  The Helix Network Supervisor will create a directory " +
                "called Helix with several sub-directories.";

            wizardTreeView.Nodes.Clear();
            rootNode = fileExplorer.PopulateTreeView(wizardTreeView);
            resultTextBox.Text = _helixPath;
        }

        private bool Step2_Validation()
        {
            string testPath = _helixPath + @"\Helix";

            if (Directory.Exists(testPath))
            {
                DialogResult dr = MessageBox.Show("The Helix directory already exists in this directory." +
                    "\rThe Helix Network Supervisor will create several new subdirectories in this directory." +
                    "\r\rDo you still want to use this directory?",
                    "Helix Setup Wizard Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                if (dr == DialogResult.Yes)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return true;
        }

        private void Step3()
        {
            currentStep = 3;
            resultLabel.Text = "Audio Files Path";

            groupBox1.Text = "Step 3 of 4: Audio Files Path";

            whatRichTextBox.Text = "Where do you store your audio files on your hard drive?  " +
                "If you don't store them in a central directory then you can skip this step.";

            whyRichTextBox.Text = "The Playlist builder will use this directory to help you build the playlists.  " +
                "You will still be able to search your hard drive if you don't specify a directory here.";

            wizardTreeView.Nodes.Clear();
            rootNode = fileExplorer.PopulateTreeView(wizardTreeView);
            resultTextBox.Text = _audioPath;
        }

        private void Step4()
        {
            //skipButton.DialogResult = DialogResult.OK;
            nextButton.DialogResult = DialogResult.OK;
            nextButton.Text = "Finish";
            nextButton.Enabled = false;
            skipButton.Enabled = false;

            currentStep = 4;
            resultLabel.Text = "MP3 Chip Version";

            groupBox1.Text = "Step 4 of 4: MP3 Chip Version";

            whatRichTextBox.Text = "Which version of MP3 Player are you using?";

            whyRichTextBox.Text = "This will enable proper configuration of the MP3 player.";

            wizardTreeView.Visible = false;
            mp3GroupBox.Visible = true;
        }
        #endregion Wizard Steps

        #region Events

        private void wizardTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            TreeNode currNode = e.Node;

            string currentPath = e.Node.FullPath;

            if (rootNode == currNode)
            {
                return;
            } // End if

            currentPath = currentPath.Remove(0, 12);

            currNode.Nodes.Clear();

            if (Directory.Exists(currentPath) == false)
            {
                return;
            } // End if
            else
            {
                TreeNodeCollection fileCollection = currNode.Nodes;
                fileExplorer.PopulateFolders(currentPath, fileCollection);
            } // End else
        }

        private void wizardTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode currNode = e.Node;

            if (rootNode == currNode)
            {
                resultTextBox.Text = completePath;
                return;
            } // End if

            completePath = currNode.FullPath;

            completePath = completePath.Remove(0, 12);

            completePath = completePath.Remove(2, 1);

            resultTextBox.Text = completePath;
        }

        private void skipButton_Click(object sender, EventArgs e)
        {
            switch (currentStep)
            {
                case 1:
                    Step2();
                    break;

                case 2:
                    Step3();
                    break;

                case 3:
                    Step4();
                    break;
            }
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            switch (currentStep)
            {
                case 0:
                    Step1();
                    break;

                case 1:
                    _vixenPath = resultTextBox.Text;
                    if (Step1_Validation())
                    {
                        Step2();
                    }
                    else
                    {
                        Step1();
                    }
                    break;

                case 2:
                    _helixPath = resultTextBox.Text;
                    if (!Step2_Validation())
                    {
                        Step2();
                    }
                    else
                    {
                        Step3();
                    }
                    break;

                case 3:
                    _audioPath = resultTextBox.Text;
                    Step4();
                    break;
            }
        }

        private void previousButton_Click(object sender, EventArgs e)
        {
            switch (currentStep)
            {
                case 1:
                    Step0();
                    break;

                case 2:
                    Step1();
                    break;

                case 3:
                    Step2();
                    break;

                case 4:
                    Step3();
                    break;
            }
        }

        private void vs1002RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (vs1002RadioButton.Checked == true)
            {
                _mp3ChipVersion = "2";
                resultTextBox.Text = "VS1002";
                nextButton.Enabled = true;
            }
        }

        private void vs1053RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (vs1053RadioButton.Checked == true)
            {
                _mp3ChipVersion = "4";
                resultTextBox.Text = "VS1053";
                nextButton.Enabled = true;
            }
        }

        #endregion Events

        #region Properties

        public string vixenPath
        {
            get { return _vixenPath; }
            set { _vixenPath = value; }
        }

        public string helixPath
        {
            get { return _helixPath; }
            set { _helixPath = value; }
        }

        public string audioPath
        {
            get { return _audioPath; }
            set { _audioPath = value; }
        }

        public string mp3ChipVersion
        {
            get { return _mp3ChipVersion; }
            set { _mp3ChipVersion = value; }
        }

        #endregion Properties

    }
}
